﻿using System.Windows;
using Microsoft.Win32;

namespace Monti.MVVM.Services
{
   /// <summary>
   /// A basic open file service.
   /// </summary>
   public class OpenFileService : IOpenFileService
   {
      /// <summary>
      /// Gets or Sets the open file dialog will allow multiple selection or not.
      /// </summary>
      public bool AllowMultipleSelection { get; set; }

      /// <summary>
      /// Gets or Sets the initial directory of the Open file dialog.
      /// </summary>
      public string InitialDirectory { get; set; }

      /// <summary>
      /// Gets or Sets the Open file dialog title.
      /// </summary>
      public string Title { get; set; }

      /// <summary>
      /// Gets or Sets the filter.
      /// </summary>
      public string Filter { get; set; }

      /// <summary>
      /// Gets the file name selected.
      /// </summary>
      public string FileName { get; private set; }

      /// <summary>
      /// Gets the files name selected.
      /// </summary>
      public string[] FileNames { get; private set; }

      /// <summary>
      /// Shows the dialog.
      /// </summary>
      /// <returns>The dialog's result.</returns>
      public bool ShowDialog ()
      {
         var ofd = new OpenFileDialog {Filter = this.Filter, Multiselect = this.AllowMultipleSelection};
         if (!string.IsNullOrEmpty (this.Title))
            ofd.Title = this.Title;
         if (!string.IsNullOrEmpty (this.InitialDirectory))
            ofd.InitialDirectory = this.InitialDirectory;
         var result = ofd.ShowDialog (Application.Current.MainWindow);
         if (result.Value)
         {
            this.FileName = ofd.FileName;
            this.FileNames = ofd.FileNames;
         }
         return result.Value;
      }
   }
}
